const db = wx.cloud.database()
const _ = db.command
Component({
	/**
	 * 组件的属性列表 
	 */
	properties: {
		openid:String
	},

	/**
	 * 组件的初始数据
	 */
	data: {
		teams: [],
		indexList: [],
		index:''
	},
	lifetimes:{
		attached(){
			console.log("组件触发")
			wx.cloud.callFunction({
				name: "getopenid"
			}).then(eres => {
				console.log(eres.result.openid)
				let openid = eres.result.openid
				db.collection('team').where({ _openid: openid }).get().then(res => {
					console.log(res)
					let statusList = []
					let indexList = []
					for (let index in res.data) {
						indexList.push(0)
					}
					this.setData({
						indexList: indexList,
						teams: res.data
					})
				})
			})
		}
	},
	/**
	 * 组件的方法列表
	 */ 
	methods: {
		selected(e) {
			// console.log(e.currentTarget.dataset.index)
			let indexList = []
			for (let i in this.data.indexList) {
				indexList.push(0)
			}
			indexList[e.currentTarget.dataset.index] = 1
			this.setData({
				index:e.currentTarget.dataset.index,
				indexList: indexList
			})
		},
		invite(){
			if (this.data.teams[this.data.index].beinvited.includes(this.data.openid)){
				return 0
			}
			db.collection("team").doc(this.data.teams[this.data.index]._id).update({
				data:{
					beinvited: _.addToSet(this.data.openid)
				}
			}).then(res=>{
				console.log(res)
				if(res.stats.updated!=0){
					return 1
				}
				return -1
			})
		}
	}
})
